#include "srcryptolaliawidget.h"

#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QRegExpValidator>

#include <OEMessageBox>

// private lib
#include <auditapi.h>


// 密文长度
const int& CRYPTOLALIA_SIZE = 12;
const int& KEY_SIZE = 12;

//SRCryptolaliaWidget::SRCryptolaliaWidget(QWidget *parent)
//    : QWidget(parent), strCryptolalia_(""){

//    setWindowTitle(tr("密语验证页面"));

//    /////////////////布局初始化////////////////
//    // 主布局
//    QVBoxLayout* layout = new QVBoxLayout();
//    // 密钥
//    QHBoxLayout* layout_key = new QHBoxLayout();
//    // 密语格子
//    QGridLayout* layout_crptolalia_edit = new QGridLayout();
//    // 密语
//    QHBoxLayout* layout_crptolalia = new QHBoxLayout();
//    // 生成方式
//    QHBoxLayout* layout_way = new QHBoxLayout();


//    /////////////////按钮////////////////
//    btnNext_ = new QPushButton(tr("下一步"));
//    btnRand_ = new QPushButton(tr("随机生成"));
//    btnHand_ = new QPushButton(tr("手动输入"));
//    btnHand_->hide();

//    /////////////// 密语框 ///////////////////
//    // 新增 6 个密语框，并加以布局
//    QLineEdit* edit = Q_NULLPTR;
//    QRegExp reg_cryptolalia("^[\u4e00-\u9fa5]$");
//    QRegExpValidator* validator_cryptolalia = new QRegExpValidator(reg_cryptolalia, this);
//    for (int r = 0; r < 1; ++r) {
//        for (int c = 0; c < 6; ++c) {
//            edit = new QLineEdit();
//            layout_crptolalia_edit->addWidget(edit,r,c);
//            edit->setValidator(validator_cryptolalia);
//            edit->setFixedSize(36,30);
//            listEdit_.push_back(edit);
//        }
//    }

//    labTitle_ = new QLabel(tr("复审密语 A"));

//    labKey_ = new QLabel(tr("复审密码 A"));

//    editKey_ = new QLineEdit();

//    QLabel* lab_remind = new QLabel("<p style='text-align:center;line-height:18px;color:rgb(204,204,204)'><b style='color:red'>温馨提示：</b>请务必抄写好密语，并秘密的保管！<br/>
//                                    密语是恢复资产的唯一法，密语丢失，资产难保！<br/>
//                                    密语=资产，密语AB及密码AB，切记不可由同一人掌握！</p>");



//    // 输入限制规则
//    QRegExp reg_key("[0-9]{1,6}");
//    QRegExpValidator* validator_key = new QRegExpValidator(reg_key, this);
//    editKey_->setValidator(validator_key);
//    editKey_->setEchoMode(QLineEdit::Password);
//    editKey_->setPlaceholderText(tr("请输入6位数字密码"));

//    /// 控件大小设置
//    btnNext_->setFixedSize(100,40);
//    btnRand_->setFixedSize(88,28);
//    btnHand_->setFixedSize(88,28);
//    editKey_->setFixedSize(161,30);
//    lab_remind->setFixedSize(300,80);

////    setMinimumSize(660,550);
////    setFixedSize(660,550);

//    /////////////////布局////////////////

//    layout_way->addWidget(btnRand_);
//    layout_way->addWidget(btnHand_);

//    layout_key->addWidget(labKey_);
//    layout_key->addWidget(editKey_,1,Qt::AlignLeft);

//    layout_crptolalia->addWidget(labTitle_);
//    layout_crptolalia->addLayout(layout_crptolalia_edit);

//    layout->addLayout(layout_way);
//    layout->addLayout(layout_crptolalia);
//    layout->addLayout(layout_key);
//    layout->addWidget(btnNext_,0,Qt::AlignHCenter);

//    layout->addWidget(lab_remind,0,Qt::AlignHCenter);
//    layout->setMargin(0);
//    setLayout(layout);
//    /////////////////信号链接///////////////////
//    connect(btnRand_,&QPushButton::clicked,
//            this, [this](){
//        char ch[64] = {};
//        int ch_size = 0;
//        // 获得随机文字
//        if (0 != apiRandChinese(this->listEdit_.count()*2,
//                                0,ch,&ch_size)) {
//            // 触发警告
//            qDebug() << "api rand chinese error";
//        }
//        QString rand_text = QString::fromUtf8(ch,ch_size);

//        // 从随机的中间取
//        int i = listEdit_.count()
//                - listEdit_.count() / 2;

//        for (auto item : listEdit_) {
//            memset(ch,0,sizeof(ch));
//            ch_size = 0;
//            // 设置 edit
//            item->setText(rand_text.at(i++));
//        }
//    });

//    connect(btnHand_,&QPushButton::clicked,
//            this,[this](){
//        for(auto item : this->listEdit_)
//            item->clear();
//    });


//    connect(btnNext_,SIGNAL(clicked()),
//            this,SLOT(onSubmitCryptolalia()));

//}

SRCryptolaliaWidget::SRCryptolaliaWidget(QWidget *parent)
    :QWidget(parent), strCryptolalia_("")
{
    setWindowTitle(tr("密语验证页面"));

    /////////////////布局初始化////////////////
    // 主布局
    QVBoxLayout* layout = new QVBoxLayout();
    QVBoxLayout* layout_edit = new QVBoxLayout();
    QHBoxLayout* layout_main = new QHBoxLayout();
    // 密钥
    QVBoxLayout* layout_key = new QVBoxLayout();
    QHBoxLayout* layout_key1 = new QHBoxLayout();
    QHBoxLayout* layout_key2 = new QHBoxLayout();
    // 密语
    QVBoxLayout* layout_crptolalia = new QVBoxLayout();
    QHBoxLayout* layout_crptolalia1 = new QHBoxLayout();
    QHBoxLayout* layout_crptolalia2 = new QHBoxLayout();


    // 进度
    labSchedule_ = new QLabel();
    labSchedule_->setPixmap(QPixmap(":/register/nvgn2"));
    labSchedule_->setFixedWidth(465);

    btnRand_ = new QPushButton(tr("随机生成"));
    btnRand_->setFixedSize(88,28);

    // 密语
    QLabel* lab_crptolalia1 = new QLabel(tr("初审密语A:"));
    QLabel* lab_crptolalia2 = new QLabel(tr("确认密语A:"));
    QLineEdit* edit_crptolalia1 = new QLineEdit("");
    QLineEdit* edit_crptolalia2 = new QLineEdit("");

    QRegExp reg_cryptolalia("^[\u4e00-\u9fa5]{0,6}$");
    QRegExpValidator* validator_cryptolalia = new QRegExpValidator(reg_cryptolalia, this);
    edit_crptolalia1->setValidator(validator_cryptolalia);
    edit_crptolalia2->setValidator(validator_cryptolalia);
    edit_crptolalia1->setPlaceholderText(tr("请输入 6 个中文"));
    edit_crptolalia2->setPlaceholderText(tr("请确认 6 个中文"));

    layout_crptolalia1->addWidget(lab_crptolalia1,0,Qt::AlignLeft);
    layout_crptolalia1->addWidget(edit_crptolalia1,0,Qt::AlignLeft);
    layout_crptolalia1->addWidget(new QLabel("<b style='color:red'>非 公 开</b>"),0,Qt::AlignRight);
//    layout_crptolalia1->addWidget(btnRand_,0,Qt::AlignLeft);

    layout_crptolalia2->addWidget(lab_crptolalia2,0,Qt::AlignLeft);
    layout_crptolalia2->addWidget(edit_crptolalia2,0,Qt::AlignLeft);
    layout_crptolalia2->addWidget(new QLabel("<b style='color:red'>　　　</b>"),0,Qt::AlignRight);

    layout_crptolalia->addLayout(layout_crptolalia1);
    layout_crptolalia->addSpacing(15);
    layout_crptolalia->addLayout(layout_crptolalia2);


    // 密码
    QLabel* lab_key1 = new QLabel(tr("初审密码A:"));
    QLabel* lab_key2 = new QLabel(tr("确认密码A:"));
    QLineEdit* edit_key1 = new QLineEdit("");
    QLineEdit* edit_key2 = new QLineEdit("");
    QRegExp reg_num("^[0-9]{0,6}$");
    QRegExpValidator* validator_key =
            new QRegExpValidator(reg_num, this);
    edit_key1->setValidator(validator_key);
    edit_key2->setValidator(validator_key);
    edit_key1->setEchoMode(QLineEdit::EchoMode::Password);
    edit_key2->setEchoMode(QLineEdit::EchoMode::Password);
    edit_key1->setPlaceholderText(tr("请输入 6 个数字"));
    edit_key2->setPlaceholderText(tr("请确认 6 个数字"));

    layout_key1->addWidget(lab_key1,0,Qt::AlignLeft);
    layout_key1->addWidget(edit_key1,0,Qt::AlignLeft);
    layout_key1->addWidget(new QLabel("<b style='color:red'>财务使用</b>"),0,Qt::AlignRight);

    layout_key2->addWidget(lab_key2,0,Qt::AlignLeft);
    layout_key2->addWidget(edit_key2,0,Qt::AlignLeft);
    layout_key2->addWidget(new QLabel("<b style='color:red'>　　　</b>"),0,Qt::AlignRight);


    layout_key->addLayout(layout_key1);
    layout_key->addSpacing(15);
    layout_key->addLayout(layout_key2);

    // 输入区布局
    layout_edit->addLayout(layout_crptolalia);
    layout_edit->addSpacing(30);
    layout_edit->addLayout(layout_key);

    edit_crptolalia1->setFixedSize(199,30);
    edit_crptolalia2->setFixedSize(199,30);
    edit_key1->setFixedSize(199,30);
    edit_key2->setFixedSize(199,30);



    // 提示
    QLabel* lab_remind = new QLabel("<p style='text-align:center;line-height:18px;color:rgb(204,204,204)'><b style='color:red'>温馨提示：</b>请务必抄写好密语，并<br/>秘密的保管！\
                                    <br/>密语是恢复资产的唯一法，密语丢<br/>失，资产难保！<br/>\
                                    密语=资产，密语AB及密码AB，切<br/>记不可由同一人掌握！</p>");
    lab_remind->setFixedSize(200,180);

    // 下一步
    btnNext_ = new QPushButton(tr("下一步"));
    btnNext_->setFixedSize(100,40);

    layout_main->addStretch();
    layout_main->addLayout(layout_edit);
    layout_main->addSpacing(30);
    layout_main->addWidget(lab_remind);
    layout_main->addStretch();

    layout->addWidget(labSchedule_,0,Qt::AlignHCenter);
    layout->addSpacing(50);
    layout->addWidget(btnRand_, 0, Qt::AlignHCenter|Qt::AlignTop);
    layout->addSpacing(30);
    layout->addLayout(layout_main);
    layout->addSpacing(30);
    layout->addWidget(btnNext_,0, Qt::AlignHCenter);
    layout->addStretch();

    setLayout(layout);


    /// link
    // 随机密语
    connect(btnRand_,&QPushButton::clicked,
            this, [edit_crptolalia1](){
        char ch[64] = {};
        int ch_size = 0;
        // 获得随机文字
        if (0 != apiRandChinese(6,0,ch,&ch_size)) {
            // 触发警告
            qInfo("api rand chinese error");
        }
        QString rand_text = QString::fromUtf8(ch,ch_size);
        edit_crptolalia1->setText(rand_text);
    });

    // 下一步
    connect(btnNext_, &QPushButton::clicked,
            this, [=]{
        QString info;
        do {
            if (edit_crptolalia1->text().length() < 6) {
                edit_crptolalia1->setFocus();
                info = tr("密语不能小于6位");
                break;
            }
            else if (edit_crptolalia2->text()
                     != edit_crptolalia1->text()) {
                edit_crptolalia2->setFocus();
                info = tr("密语输入不一致");
                break;
            }
            else if (edit_key1->text().length() < 6) {
                edit_key1->setFocus();
                info = tr("密码不能小于6位");
                break;
            }
            else if (edit_key2->text()
                     != edit_key1->text()) {
                edit_key2->setFocus();
                info = tr("密码输入不一致");
                break;
            }

            this->strCryptolalia_ += edit_crptolalia1->text();
            this->strKey_ += edit_key1->text();

            if (strCryptolalia_.length() < 12) {
                lab_crptolalia1->setText(tr("复审密语B:"));
                lab_crptolalia2->setText(tr("确认密语B:"));
                lab_key1->setText(tr("复审密码B:"));
                lab_key2->setText(tr("确认密码B:"));
                edit_crptolalia1->clear();
                edit_crptolalia2->clear();
                edit_key1->clear();
                edit_key2->clear();
                if (registerMode_ == 1) {
                    labSchedule_->setPixmap(QPixmap(":/register/nvgn_3"));
                }
                else {
                    labSchedule_->setPixmap(QPixmap(":/register/nvgn3"));
                }

            }
            else {
                emit sigCryptolaliaFinshed(strCryptolalia_, strKey_);
            }

        } while(false);


        if (!info.isEmpty())
            OEMessageBox::BriefMessage(this,info);
    });
}

SRCryptolaliaWidget::~SRCryptolaliaWidget()
{
#ifdef QT_DEBUG
    qDebug("密语注册窗口被析构");
#endif
}

void SRCryptolaliaWidget::setRegisterMode(int _mode)
{
    if (_mode == 1) {
        labSchedule_->setPixmap(QPixmap(":/register/nvgn_2"));
        registerMode_ = _mode;
    }

    return;
}


//void SRCryptolaliaWidget::onSubmitCryptolalia(void) {
//    QString temp_cryptolalia;
//    // 获得密文
//    for(auto item : this->listEdit_)
//        temp_cryptolalia += item->text();

//    // 检验密文
//    if (temp_cryptolalia.size() != this->listEdit_.size()
//            || this->lab_key1->text().size() != (KEY_SIZE >> 1)) {
//        for (auto item : this->listEdit_)
//            if (item->text().isEmpty()) {
//                item->setFocus();
//                break;
//            }
//        OEMessageBox::BriefMessage(this,tr("密语不能为空"));
//        return;
//    }



//    // 断开现有槽
//    disconnect(btnNext_,SIGNAL(clicked()),
//               this,SLOT(onSubmitCryptolalia()));


//    // 进入密文确认
//    callConfirmCryptolalia(temp_cryptolalia,this->lab_key1->text());

//}

//void SRCryptolaliaWidget::callConfirmCryptolalia(const QString &_complete,
//                                                 const QString &_key)
//{
//    // 清理Edit
//    for(auto item : this->listEdit_)
//        item->clear();

//    this->lab_key1->clear();

//    labTitle_->setText(tr("确认密语"));
//    labKey_->setText(tr("确认密码"));

//    btnRand_->hide();

//    static QMetaObject::Connection link_next;

//    link_next = connect(btnNext_, &QPushButton::clicked,
//            this,[=](){
//        QString temp_cryptolalia;

//        // 获得密文
//        for(auto item : this->listEdit_)
//            temp_cryptolalia += item->text();

//        // 检验密文
//        if (temp_cryptolalia.size() != this->listEdit_.size()
//           || temp_cryptolalia != _complete
//           || this->lab_key1->text() != _key) {
//            // 弹出提示框
//            OEMessageBox::BriefMessage(this,tr("密语输入有误"));
//            return;
//        }

//        // 断开现有槽
//        disconnect(link_next);
//        // 追加密文
//        this->strCryptolalia_ += _complete;
//        // 追加key
//        this->strKey_ += _key;

//        if (this->strCryptolalia_.size() == CRYPTOLALIA_SIZE
//             && this->strKey_.size() == KEY_SIZE) {
//            // 12个密语确认完毕 进入下个页面
//#ifdef QT_DEBUG
//            qDebug() << "12个密语与密钥确认完毕 进入下个页面";
//#endif
//            emit sigCryptolaliaFinshed(this->strCryptolalia_,this->strKey_);
//            return;
//        }

//        callSubmitCryptolalia();

//    });
//}

//void SRCryptolaliaWidget::callSubmitCryptolalia()
//{
//    /// 进入密语填写页面
//    this->labTitle_->setText(tr("初审密语 B"));
//    this->labKey_->setText(tr("初审密码 B"));

//    for(auto item : this->listEdit_)
//        item->clear();

//    this->lab_key1->clear();

//    connect(this->btnNext_,SIGNAL(clicked()),
//            this,SLOT(onSubmitCryptolalia()));


//    this->btnRand_->show();

//#ifdef QT_DEBUG
//    qDebug() << this->strCryptolalia_;
//#endif
//}


